package demo12;

import java.lang.reflect.Method;

/**
 * @program: qy158
 * @description:
 * @author: 闫克起2
 * @create: 2022-10-27 10:40
 **/
public class Test {
    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("demo12.Cat");
//        1. Method getDeclaredMethod(String name, 类<?>... parameterTypes)
//        根据方法名和参数获取本类中指定的方法对象
        Method fun1 = aClass.getDeclaredMethod("fun1");

        //普通: 对象名.方法(实参)  反射: 方法对象。invoke(对象名,实参)
        Object o = aClass.newInstance();
        Object result = fun1.invoke(o);//方法的回调
        System.out.println(result);

        Method fun2 = aClass.getDeclaredMethod("fun2", String.class, Integer.class);
        Object r = fun2.invoke(o, "刘德华", 28);
        System.out.println(r);

    }
}

class Animal{
     public void show(){
         System.out.println("动物的show方法");
     }
     private void print(){
         System.out.println("动物的print方法");
     }
}
class Cat extends  Animal{
     public void fun1(){
         System.out.println("Cat中的fun1方法");
     }
     public String fun2(String name,Integer age){
         System.out.println("cat中fun2方法"+name);
         return "呵呵呵呵";
     }
}
